<?php
namespace app\index\controller;
use think\Controller;
use think\Db;
class Index extends Controller{
    public $openid;
    public $tel;
    public $appid;
    public function __construct(){
        parent::__construct();
        if( !session('?openid') ){
            //获取openid
            if(!$code = input('code')){
                echo '<h2>code获取失败</h2>';
            }else{
                $info = action('wx/get_openid');
                session('openid',$info['openid']);
                cache($info['openid'],serialize($info));
            }
        }

        $this->appid = config('wx_appid');
    }

    public function del(){
        Db::execute("delete from T_Sys_User where FMobile='13702387651' or FMobile='14778659170'");
        dump(Db::query("select * from T_Sys_User"));
    }

    /**手机短信验证
     * @return mixed
     */
    public function sms(){
        //判断有没有绑定手机号
        $openid = request()->session('openid');
        $teltype = Db::query("select FId from T_Sys_User where FOpenId='{$openid}'");
        //如果已经绑定跳转用户中心
        if($teltype){
            $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid={$this->appid}&redirect_uri=http%3A%2F%2Fanan.zjqingchun.com%2Findex%2Findex%2Fuser&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";
            $this->redirect($url);
            exit;
        }

        return $this->fetch();

    }

    /**
     * 绑定手机号码
     */
    public function bind_tel(){
        $tel = input('tel');
        $openid = request()->session('openid');
        $info = unserialize(cache($openid));

        $sql = "insert into T_Sys_User (FMobile,FOpenId,FNickName,FSex,FHeadImgUrl,FCountry,FProvince,FCity) 
                    values 
                    ('{$tel}','{$info['openid']}','{$info['nickname']}',{$info['sex']},'{$info['headimgurl']}','{$info['country']}','{$info['province']}','{$info['city']}')";

        if(Db::execute($sql)){
            session('openid',$info['openid']);
            $this->redirect('bind_card');
        }else{
            $this->error('绑定手机号码失败');
        }
    }

    /**验证手机号码
     * @return array
     */
    public function verify_tel(){
        $tel = input('tel');
        //检测号码是否绑定
        $teltype = Db::query("select FId from T_Sys_User where FMobile='{$tel}'");
        if($teltype){
            return ['code'=>1,'msg'=>'该手机号已经绑定过'];
        }else{
            return ['code'=>0,'msg'=>''];
        }
    }

    /**验证手机短信验证码
     * @return array
     */
    public function verify_code(){
        $tel = input('tel');
        $code = input('code');
        $oldcode = cookie($tel.'_code');
        return ['code'=>0,'msg'=>''];
        if($oldcode !== $code){
            return ['code'=>1,'msg'=>'验证码不正确'];
        }else{
            return ['code'=>0,'msg'=>''];
        }
    }

    /**
     * 获取短信验证码
     */
    public function get_code(){
        $tel = input('tel');

        //检测号码是否绑定
        $teltype = Db::query("select FId from T_Sys_User where FMobile='{$tel}'");
        if($teltype){
            return ['code'=>1,'msg'=>'该手机号已经绑定过'];
        }
        $mb = config('ZC_MB_ID');
        $sum = rand(100000,999999);
        $res = action('index/alisms/sendSms',[$tel,$mb,$sum]);
        if($res['Code'] == 'OK'){
            //验证码存入cookie
            cookie($tel.'_code',$sum,300);
            $code = 0;
            $msg = '';
        }else{
            $code = 1;
            $msg = $res['Message'];
        }
        $list = [
            'code' => $code,
            'msg'  => $msg
        ];
        return $list;
    }

    /**
     * 绑卡
     */
    public function bind_card(){
        return $this->fetch();
    }


    /**获取班级
     * @return array
     */
    public function get_class(){
        $card = input('card');
        //$card = 1332334533;

        //获取卡信息
        $info = Db::query("select FId,FSchoolId,FClassId,FName from T_Bd_Card where FIdNumber='{$card}' and FStateId=1");
        if(!$info){
            return ['code'=>1,'msg'=>'该卡号不存在或者没启用'];
        }
        //halt($info);
        $fid = $info[0]['FId'];
        $sid = $info[0]['FSchoolId'];
        $fname = $info[0]['FName'];

        //检测卡是否绑定
        $openid = session('openid');
        $user = Db::query("select FId from T_Sys_User where FOpenId='{$openid}'");
        $userid = $user[0]['FId'];
        $sql = "select FId from T_Bd_UserCard where FCardId=$fid and FUserId='{$userid}'";
        $cardtype = Db::query($sql);

        if($cardtype){
            return ['code'=>2,'msg'=>'该卡已经绑定'];
        }
        //获取学校所有班级
        $class = Db::query("select FId,FName from T_Bd_Class where FSchoolId={$sid}");

        $data =  [
            'code' => 0,
            'msg' => '成功获取数据',
            'list' => [
                'userid' => $userid,
                'fname'  => $fname,
                'fid'    => $fid,
                'data'   => $class
            ]
        ];
        //halt($data);
        return $data;

    }

    /**
     * 头像上传
     */
    public function upload_avatar(){
        return $this->fetch();
    }

    /**
     * 个人中心
     */
    public function user(){
        return $this->fetch();
//        if( session('?openid') ){
//            //跳到个人中心
//            $this->redirect('user');
//        }else{
//            if(!$code = input('code')){
//                echo '<h2>code获取失败</h2>';
//            }else{
//                //获取微信用户信息openid和详细信息
//                $info = action('wx/get_openid');
//                $key = time()."open";
//                cache($key,serialize($info));
//                $this->assign('key',$key);
//                return $this->fetch();
//            }
//        }
    }

    /**
     * 个人中心
     */
    public function ldentity(){
        return $this->fetch();
    }

    public function upload1(){
        return $this->fetch();
    }
    public function upload2(){
        $img = isset($_POST['img'])? $_POST['img'] : '';

// 获取图片
        list($type, $data) = explode(',', $img);

// 判断类型
        if(strstr($type,'image/jpeg')!=''){
            $ext = '.jpg';
        }elseif(strstr($type,'image/gif')!=''){
            $ext = '.gif';
        }elseif(strstr($type,'image/png')!=''){
            $ext = '.png';
        }

// 生成的文件名
        $photo = "./upload/".time().$ext;

// 生成文件
        file_put_contents($photo, base64_decode($data), true);

// 返回
        header('content-type:application/json;charset=utf-8');
        $res = array('img'=>$photo);
        echo json_encode($res);

    }
}
